<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/26 0026
 * Time: 下午 2:42
 */
namespace app\message\controller\chat;
use app\common\api\message\IChat;
use app\message\validate\Vali_chat_mes;
use app\message\validate\ValidateFun;
use think\Db;

class Chat_mes implements IChat{
    //接收参数
    private $data=[];
    /**
     * 初始化配置
     * @param array $config
     */
    public function __construct(array $config = []){
        $this->data = array_filter_full(array_merge($this->data,$config));
    }
    public function chat_add()
    {
        // TODO: Implement chat_add() method.
        $session = session('admin_info');
        $default =[
            'del'   =>1,
            'createtime'=>date('Y-m-d H:i:s'),
            'uid'   =>$session['id']
        ];
        $data = array_filter_full(array_merge($default,$this->data));
        unset($data['id']);
        #验证参数
        $vali = Vali_chat_mes::c_mes_add();
        $validate = new ValidateFun($vali['rule'],$vali['message']);
        if (!$validate->check($data)){
            $err = $validate->getError();
            return ['code'=>$err,'msg'=>config("msg.$err")];
        }
        #添加
        $add = new \app\message\model\Chat_mes();
        $res = $add ->add($data);
        #返回结果
        if ($res>0){
            return ['code'=>200,'msg'=>config('msg.200'),'data'=>$res];
        }else{
            return ['code'=>10006,'msg'=>config('msg.10006'),'data'=>$res];
        }
    }
    public function chat_edit()
    {
        // TODO: Implement chat_edit() method.
        $data = $this->data;
        #验证参数
        $vali = Vali_chat_mes::c_mes_edit();
        $validate = new ValidateFun($vali['rule'],$vali['message']);
        if (!$validate->check($data)){
            $err = $validate->getError();
            return ['code'=>$err,'msg'=>config("msg.$err")];
        }
        #删除
        $del = new \app\message\model\Chat_mes();
        $res = $del->del($data);
        #结果
        if ($res>0){
            return ['code'=>200,'msg'=>config('msg.200')];
        }else{
            return ['code'=>10007,'msg'=>config('msg.10007')];
        }
    }
    public function chat_list()
    {
        // TODO: Implement chat_list() method.
        $default =[
            'curr'     =>1,
            'limits'   =>5,
        ];
        $data = array_filter_full(array_merge($default,$this->data));
        #获取列表
        $list = new \app\message\model\Chat_mes();
        $result = $list->chat_list($data);
        #返回
        return ['code'=>200,'msg'=>config('msg.200'),'data'=>$result];
    }
    public function chat_list_child(){
        $data = $this->data;
        $field = 'm.username,cm.id,cm.content,cm.uid,cm.pid,cm.createtime';
        $list = Db::name('chat_message')
            ->alias('cm')
            ->join('member m','m.id=cm.uid')
            ->field($field)
            ->where(['pid'=>$data['id']])
            ->order('cm.createtime')
            ->select();
        #返回
        return ['code'=>200,'msg'=>config('msg.200'),'data'=>$list];
    }
}